(ns net.marigolda.templating-start
   (:use [net.cgrand.enlive-html :as html]
         [ring.adapter.jetty :only [run-jetty] :as jetty]
         [ring.util.response :only [response]]
         [compojure.core :only [defroutes GET POST wrap!]]
         [net.marigolda.gui-utils]
         [net.marigolda.scraping-start]
         [net.marigolda.data]
         [net.marigolda.so-start])
   (:require [compojure.route :as route]))

;;site
(def site-name "Look Ma No Hands")

;; util
;; getting pages content
(defn page-content [page]
   (html/select (html/html-resource (str "public/" page "-content.html")) [:#page]))

;; snippets
;; sidebar snippet
(html/defsnippet sidebar "public/layout.html" [:#sidebar] 
   [{:keys [sidebar-box-one sidebar-box-two sidebar-box-three]}]
   [:#sidebar-box-one] (html/content sidebar-box-one)
   [:#sidebar-box-two] (html/content sidebar-box-two)
	 [:#sidebar-box-three] (html/content sidebar-box-three))

;; navigation current item
(html/defsnippet navigation-current "public/layout.html" [:#nav]
   [current]
   [(attr= :href (str "/" current))] (html/set-attr :class "current"))

;; post snipets
(html/defsnippet post-model "public/layout.html" [:.post]
   [{:keys [title date month author body link]}]
   [:.post :.title :a] (html/do-> (html/set-attr :name link) (html/content title))
   [:.post :.date] (html/content date)
   [:.post :.month] (html/content month)
   [:.post :.postinfo :em] (html/content author)
   [:.post :.post-body] (html/html-content body))

;; latest posts
(html/defsnippet latest-link "public/layout.html" [:#latest :li]
   [{:keys [title link]}]
   [:a] (html/do-> (html/set-attr :href (str "/#" link)) (html/content title)))

;; user details
(html/deftemplate user-details "public/user-details.html"
   [{:keys [user-id dname emailh nof-qs nof-qs-view nof-qs-up nof-qs-down nof-qs-fav nof-as nof-as-view nof-as-up nof-as-down nof-as-accept avg-q-up avg-q-down avg-a-up avg-a-down avg-q-view avg-a-view rel-q-fav rel-a-accept]} title]
   [:.username] (html/do-> (html/set-attr :id user-id) (html/content dname))
   [:.user_img :img] (html/set-attr :src (str "http://www.gravatar.com/avatar/" emailh "?s=50"))
   [:.full-profile] (html/set-attr :href (str "http://stackoverflow.com/users/" user-id))
   [:.q_num] (html/content (str nof-qs))
   [:.a_num] (html/content (str nof-as))
   [:.QsBox.total :.views] (html/content (str nof-qs-view))
   [:.QsBox.total :.favs] (html/content (str nof-qs-fav))
   [:.QsBox.total :.ups] (html/content (str nof-qs-up))
   [:.QsBox.total :.downs] (html/content (str nof-qs-down))
   [:.AsBox.total :.views] (html/content (str nof-as-view))
   [:.AsBox.total :.accept] (html/content (str nof-as-accept))
   [:.AsBox.total :.ups] (html/content (str nof-as-up))
   [:.AsBox.total :.downs] (html/content (str nof-as-down))
   [:.QsBox.avg :.views] (html/content (ratio-formatter avg-q-view))
   [:.QsBox.avg :.favs] (html/content (ratio-formatter rel-q-fav))
   [:.QsBox.avg :.ups] (html/content (ratio-formatter avg-q-up))
   [:.QsBox.avg :.downs] (html/content (ratio-formatter avg-q-down))
   [:.AsBox.avg :.views] (html/content (ratio-formatter avg-a-view))
   [:.AsBox.avg :.accept] (html/content (str (ratio-formatter rel-a-accept) "%"))
   [:.AsBox.avg :.ups] (html/content (ratio-formatter avg-a-up))
   [:.AsBox.avg :.downs] (html/content (ratio-formatter avg-a-down))
   [:h4.title] (html/content title))

;;sidebar defs
(defn home-sidebar []
   (sidebar {:sidebar-box-one (make-ul-with-h4 "FonForum-Master" (ff-master-top5-headlines))
             :sidebar-box-two (make-ul-with-h4 "FON Master" (fon-master-top5-headlines))})) 

(defn hci-sidebar []
   (sidebar {:sidebar-box-one (make-ul-with-h4 "[gg] CSS Design" (googlegroup-scraper "css-design"))
             :sidebar-box-two (make-ul-with-h4 "[gg] jQuery" (googlegroup-scraper "jquery-en"))})) 

(defn scm-sidebar []
   (sidebar {:sidebar-box-one (make-ul-with-h4 "[gg] SCM" (googlegroup-scraper "software-configuration-management"))}))

(defn clj-sidebar []
   (sidebar {:sidebar-box-one (make-ul-with-h4 "[gg] Clojure" (googlegroup-scraper "clojure"))
             :sidebar-box-two (make-ul-with-h4 "clojure@blogspot" (blogspot-headlines-a-tags "clojure"))
             :sidebar-box-three (make-ul-with-h4 "[gg] enlive-clj" (googlegroup-scraper "enlive-clj"))})) 

(defn ci-sidebar []
   (sidebar {:sidebar-box-one (make-ul-with-h4 "[gg] Prediction Markets" (googlegroup-scraper "prediction-markets"))
             :sidebar-box-two (make-ul-with-h4 "[gg] Genetic Programming" (googlegroup-scraper "geneticprogramming"))})) 
    
;; tempate 
;; layout
(html/deftemplate layout "public/layout.html"
   [{:keys [main sidebar title current latest]}]
   [:title] (html/content title)   
   [:#main]  (maybe-content main)
   [:#sidebar] (html/substitute (sidebar))
   [:#nav] (html/substitute (navigation-current current))
   [:#latest] (html/content latest))


;; pages
(defn home []
   (layout {:main (map #(post-model %) posts)
            :sidebar home-sidebar
            :title site-name
            :current ""
            :latest (map #(latest-link %) posts)}))

(defn hci []
   (layout {:main (page-content "hci")
           :sidebar hci-sidebar
           :title (str "hci @ " site-name)
           :current "hci"
           :latest (map #(latest-link %) posts)}))

(defn scm []
   (layout {:main (page-content "scm")
            :sidebar scm-sidebar
            :title (str "scm @ " site-name)
            :current "scm"
            :latest (map #(latest-link %) posts)}))

(defn clj []
   (layout {:main (page-content "clj")
            :sidebar clj-sidebar
            :title (str "clj @ " site-name)
            :current "clj"
            :latest (map #(latest-link %) posts)}))

(defn ci []
   (layout {:main (page-content "ci")
            :sidebar ci-sidebar
            :title (str "ci @ " site-name)
            :current "ci"
            :latest (map #(latest-link %) posts)}))

(defn page404 []
   (layout {:main (page-content "page404")
            :sidebar home-sidebar
            :title (str "404 @ " site-name)
            :current "page404"
            :latest (map #(latest-link %) posts)}))

;; routes
(defroutes routes
  (GET "/" [] (render-request home))
  (GET "/hci" [] (render-request hci))
  (GET "/scm" [] (render-request scm))
  (GET "/clj" [] (render-request clj))
  (GET "/ci" [] (render-request ci))
  (GET "/start-ci" [] (start-ci))
  (GET "/process-users" [] (process-users))
  (GET "/random-user" [] (apply str (user-details (get-random-user) "Here is one random user:")))
  (GET "/similar-user/:id" [id] (apply str (user-details (get-similar-user id) "Most similar user is:")))
  (GET "/get-user-details/:id" [id] (apply str (user-details (get-user-details id) "User details:")))
  (GET "/user-json-list" [q] (json-users-autocomplete q))
  (GET "/user-json-list" [] (json-users))
  (route/resources "/")
  (route/not-found "<center><h1><img src=\"images/404.png\" alt=\"Page not found\"/></h1></center>"))

;; suggested workaround for utf8
(defn wrap-charset [handler charset] 
  (fn [request] 
    (if-let [response (handler request)] 
      (if-let [content-type (get-in response [:headers "Content-Type"])] 
        (if (.contains content-type "charset") 
          response 
          (assoc-in response 
            [:headers "Content-Type"] 
            (str content-type "; charset=" charset))) 
        response)))) 

(defn start []
  (jetty/run-jetty (wrap! routes (:charset "utf-8")) {:port 8080 :join? false}))